package com.server.app;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.server.object.Room;
import com.server.status.SocketState;

public class RoomManager {
	private static RoomManager instance;
	public static RoomManager GetInstance() {
		if(instance == null) {
			instance = new RoomManager();
		}
		return instance;
	}
	
	private ConcurrentHashMap<Long,Map<String, HashSet<Room>>> roomMap = new ConcurrentHashMap<Long,Map<String, HashSet<Room>>>();
	
	/**
	 * 匹配房间
	 * @param appid 应用ID
	 * @param room_tag 房间标签
	 * @param player_count 最大玩家数
	 * @param uid 玩家id
	 */
	public String MatchRoom(long appid, String room_tag, int player_count, long uid) {
		String room_id = null;
		synchronized (roomMap) {
			if(roomMap.containsKey(appid)) {
				Map<String, HashSet<Room>> app_room = roomMap.get(appid);
				if(app_room.containsKey(room_tag)) {
					HashSet<Room> room_list = app_room.get(room_tag);
					for(Room room:room_list) {
						if(room.room_state == SocketState.IDLE && room.players.size() < room.player_count) {
							room.AddPlayer(uid);
							room_id = room.room_id;
							PlayerManager.GetInstance().GetPlayer(uid).room = room;
							break;
						}
					}
				}
			}
			if(room_id == null)room_id = CreateRoom(appid, room_tag, player_count, uid);
		}
		return room_id;
	}
	
	/**
	 * 加入指定房间
	 * @param appid 应用ID
	 * @param room_tag 房间标签
	 * @param player_count 最大玩家数
	 * @param uid 玩家id
	 * @param room_id 房间id
	 */
	public SocketState JoinRoom(long appid, String room_tag, long uid, String room_id) {
		SocketState rs = SocketState.NONEXISTENT;
		synchronized (roomMap) {
			if(roomMap.containsKey(appid)) {
				Map<String, HashSet<Room>> app_room = roomMap.get(appid);
				if(app_room.containsKey(room_tag)) {
					HashSet<Room> room_list = app_room.get(room_tag);
					for(Room room:room_list) {
						if(room.room_id.equals(room_id)) {
							rs = room.AddPlayer(uid);
							if(rs == SocketState.SUCCESSED) {
								room_id = room.room_id;
								PlayerManager.GetInstance().GetPlayer(uid).room = room;
							}
							break;
						}
					}
				}
			}
		}
		return rs;
	}
	
	/**
	 * 创建房间
	 * @param appid 应用ID
	 * @param room_tag 房间标签
	 * @param player_count 最大玩家数
	 * @param creator 创建者/房主
	 */
	public String CreateRoom(long appid, String room_tag, int player_count, long creator) {
		Room room = new Room(player_count, room_tag, creator, appid);
		String room_id = room.room_id;
		synchronized (roomMap) {
			//判断是否有该应用的房间
			if(roomMap.containsKey(appid)) {
				Map<String, HashSet<Room>> app_room = roomMap.get(appid);
				//判断该应用是否有该类型的房间
				if(app_room.containsKey(room_tag)) {
					app_room.get(room_tag).add(room);
				}else {
					HashSet<Room> rl = new HashSet<Room>();
					rl.add(room);
					app_room.put(room_tag, rl);
				}
			}else {
				Map<String, HashSet<Room>> app_room = new HashMap<String, HashSet<Room>>();
				HashSet<Room> rl = new HashSet<Room>();
				rl.add(room);
				app_room.put(room_tag, rl);
				roomMap.put(appid, app_room);
			}
		}
		PlayerManager.GetInstance().GetPlayer(creator).room = room;
		return room_id;
	}
	
	/**
	 * 移除房间
	 * @param appid appid
	 * @param room_tag 房间标签
	 * @param room_id 房间id
	 */
	public void RemoveRoom(long appid, String room_tag, String room_id, Room room) {
		synchronized (roomMap) {
			System.out.println(">>>>>>>>>>>>>>>>>>>>>2");
			if(roomMap.containsKey(appid)) {
				System.out.println(">>>>>>>>>>>>>>>>>>>>>3");
				Map<String, HashSet<Room>> app_room = roomMap.get(appid);
				if(app_room.containsKey(room_tag)) {
					System.out.println(">>>>>>>>>>>>>>>>>>>>>4");
					app_room.get(room_tag).remove(room);
				}
			}
		}
	}
}
